package com.wx.dao.impl;

import com.wx.dao.OrderDao;
import com.wx.domain.Order;
import com.wx.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

/**
 * @author 27250
 */
public class OrderDaoImpl implements OrderDao {
    JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int save(int patientId, String doctorId, String dateTime) {
        PreparedStatementCreator creator=new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement preparedStatement=connection.prepareStatement("insert into `order` values(?,?,?,?,?)",PreparedStatement.RETURN_GENERATED_KEYS);
                preparedStatement.setObject(1,null);
                preparedStatement.setInt(2, Integer.parseInt(doctorId));
                preparedStatement.setInt(3,patientId);
                preparedStatement.setString(4,dateTime);
                preparedStatement.setString(5,"未处理");
                return preparedStatement;
            }
        };

        GeneratedKeyHolder keyHolder=new GeneratedKeyHolder();

        jdbcTemplate.update(creator, keyHolder);

        //        jdbcTemplate.update("insert into sys_user values (null,?,?,?,?)",user.getUsername(),user.getEmail(),user.getPassword(),user.getPhoneNum());
        return Objects.requireNonNull(keyHolder.getKey()).intValue();
    }

    @Override
    public int getNumberByDoctorIdAndDateTime(String doctorId, String dateTime) {
        return jdbcTemplate.queryForObject("select count(*) from `order` where doctorId = ? and orderDate = ? ",int.class,doctorId,dateTime);
    }

    @Override
    public List<Order> getOrderListByPatientId(String patientId) {
        return jdbcTemplate.query("select * from `order` where patientId = ?",
                new BeanPropertyRowMapper<Order>(Order.class),patientId);
    }

    @Override
    public List<Order> getOrderListByDoctorId(String doctorId) {
        return jdbcTemplate.query("select * from `order` where doctorId = ?",
                new BeanPropertyRowMapper<Order>(Order.class),doctorId);
    }

    @Override
    public List<Order> getOrderListByDoctorIdAndDateTime(String doctorId, String dateTime) {
        return jdbcTemplate.query("select * from `order` where doctorId = ? and orderDate = ?",
                new BeanPropertyRowMapper<Order>(Order.class),doctorId,dateTime);
    }

    @Override
    public Order getOrderListByPatientIdAndDateTime(int patientId, String dateTime) {
        List<Order> query = jdbcTemplate.query("select * from `order` where patientId = ? and orderDate = ?",
                new BeanPropertyRowMapper<>(Order.class), patientId, dateTime);
        if (query==null || query.size()==0){
            return null;
        }
        return query.get(0);
    }

    @Override
    public void updateHandle(int id,String orderId) {
        jdbcTemplate.update("update `order` set handle= ? where orderId = ?","已处理-处方号:"+id,orderId);
    }
}
